<?php

class Default_RegisterController extends ExtLb_Controller_Action
{
	private $postTbl;
	private $commentTbl;
	private $registerTbl;
	private $guestTbl;
	
	private $registerFrm;
	
    public function init(){
        $this->loadCss('default', 'register', FOLDER_ROOT_NAME);

        $this->postTbl		= new Admin_Model_Posts();
        $this->commentTbl 	= new Admin_Model_Comments();
        $this->registerTbl	= new Admin_Model_Register();
        $this->guestTbl		= new Default_Model_Guests();
        
        $this->registerFrm	= new Admin_Form_Register();
        
        //RANDOM POST (bai viet noi bat)
    	$randPost = $this->postTbl->selectPost('bv.trangThai =1','baiVietId DESC',10)->query()->fetchAll();
    	$this->view->randPosts = $randPost;
    	
    	$this->view->comments = $this->commentTbl->selectComment(NULL,'yKienId DESC',5)->query()->fetchAll();
    	
    	date_default_timezone_set('Asia/Ho_Chi_Minh');
    }

    public function indexAction(){
        
    	$this->registerFrm->addAction('default', 'register', 'add');
    	$this->registerFrm->addSubmitButton('cmdAdd', 'Đăng ký');
    	
    	$this->view->registerFrm = $this->registerFrm;
    	
    	
    }
    
    public function addAction(){
    	if(isset($_POST['cmdAdd'])){
    		if ($this->registerFrm->isValid($this->_request->getPost())){
    			//
    			//THONG TIN CA NHAN-TO CHUC
    			$khuVucId 		= $this->_getParam('cbbLocation');
    			if ($khuVucId==0){
    				$this->jShowMessage("Hãy chọn khu vự - mặc bằng cần thuê");
    				
    				$this->_forward('index','register','default');
    				return ;
    			}
    			$nguoiDaiDien 	= ucwords($this->_getParam('txtAgent'));
    			$chucVu			= $this->_getParam('txtOffice');
    			$toChuc			= $this->_getParam('txtOrganization');
    			$diaChi			= ucwords($this->_getParam('txtAddress'));
    			$dienThoai		= $this->_getParam('txtPhone');
    			$email			= $this->_getParam('txtEmail');
    			$fax			= $this->_getParam('txtFax');
    			$quocGia		= ucwords($this->_getParam('txtNational'));
    			//
    			//THONG TIN DICH VU
    			$menu2Id			= $this->_getParam('cbbMenu2');
    			$ngayDuKien		= $this->_getParam('txtDateExpect');
    			$thoiGianThue	= $this->_getParam('txtDateRent');
    			$dienTichThue	= $this->_getParam('txtAreaRent');
    			$mucDichThue	= ucfirst($this->_getParam('txtPurposeRent'));
    			//THEM KHACH
    			$this->guestTbl->addGuest($nguoiDaiDien, $chucVu, $toChuc, $diaChi, $dienThoai, $email, $fax, $quocGia);
    			
    			$guests = $this->guestTbl->fetchCol(array('khachId'),NULL,'khachId DESC');
    			$khachId ="";
    			foreach ($guests as $guest) {
    				$khachId = $guest['khachId'];
    				break;
    			}
    			$now = getdate();
	        	$ngayDangKy = $now['mday']."/".$now['mon']."/".$now['year'];
	        	
    			$this->registerTbl->addRegister($khachId, $menu2Id,$khuVucId, $ngayDuKien, $thoiGianThue, $dienTichThue, $mucDichThue, $ngayDangKy);
    			
    			$this->jShowMessage("Đã đăng ký thành công!!!");
    			
    			$this->_request->clearParams();
    			$this->_forward('index','index','default');
    		}else{
    			$this->_redirect('default/register/');
    		}
    	}else {
    		$this->_redirect('default/register/');
    	}
    }


}

